﻿using Common.Models;
using Core.Entities.EQ;
using Infrastructure.Services;
using Infrastructure.Services.EQ;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace WebZHX.Controllers.EQ
{
    /// <summary>
    /// 设备信息控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class DeviceController : BaseController<Device, string>
    {
        private readonly DeviceService _deviceService;

        public DeviceController(
            BaseService<Device, string> service,
            DeviceService deviceService)
            : base(service)
        {
            _deviceService = deviceService;
        }

        /// <summary>
        /// 根据设备编码查询
        /// </summary>
        /// <param name="devCode">设备编码</param>
        /// <returns>设备信息列表</returns>
        [HttpGet("code/{devCode}")]
        public async Task<ActionResult<IEnumerable<Device>>> GetByDevCode(string devCode)
        {
            var result = await _deviceService.GetByDevCodeAsync(devCode);
            return Ok(result);
        }

        /// <summary>
        /// 根据设备名称模糊查询
        /// </summary>
        /// <param name="devName">设备名称</param>
        /// <returns>设备信息列表</returns>
        [HttpGet("name/{devName}")]
        public async Task<ActionResult<IEnumerable<Device>>> GetByDevName(string devName)
        {
            var result = await _deviceService.GetByDevNameAsync(devName);
            return Ok(result);
        }

        public class DeviceQueryDto
        {
            /// <summary>
            /// 分页数据
            /// </summary>
            public QueryFilter filter { get; set; }
            /// <summary>
            /// 实体数据
            /// </summary>
            public Device Device { get; set; }
        }

        /// <summary>
        /// 高级分页查询
        /// </summary>
        /// <param name="deviceQueryDto">查询参数</param>
        /// <returns>分页结果</returns>
        [HttpPost("DevicePage")]
        public async Task<ActionResult<PagedResult<Device>>> AdvancedPagedQuery([FromBody] DeviceQueryDto deviceQueryDto)
        {
            var result = await _deviceService.GetByDeviceAsync(deviceQueryDto.filter, deviceQueryDto.Device);
            return Ok(result);
        }

        /// <summary>
        /// 导入设备数据
        /// </summary>
        /// <param name="devices">设备列表</param>
        /// <returns>导入结果</returns>
        [HttpPost("ImportDevices")]
        public async Task<ActionResult> ImportDevices([FromBody] List<Device> devices)
        {
            if (devices == null || devices.Count == 0)
            {
                return BadRequest("导入数据不能为空");
            }

            var result = await _deviceService.ImportDevicesAsync(devices);
            if (result)
            {
                return Ok(new { success = true, message = "导入成功" });
            }
            else
            {
                return BadRequest(new { success = false, message = "导入失败" });
            }
        }
        /// <summary>
        /// 保存接口
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost("Save")]
        public async Task<ActionResult> GetAddUpdate(Device entity)
        {
            if (string.IsNullOrEmpty(entity.Id))
            {
                //新增
                entity.Id = Guid.NewGuid().ToString();
                entity.CreateTime = DateTime.Now;
                await Create(entity);
            }
            else
            {
                //修改
                await PartialUpdate(entity.Id,entity);
            }
            
            return Ok("保存成功");
        }

        // 注意：这里移除了新增、修改、删除的方法，因为前端不需要这些操作
    }
}